<!--我的服务器-->
<template>
  <div  class="bg" @rightBtn="rightBtn">

    <!-- 列表 -->
    <list class="list" v-if="servers && servers.length > 1">
    
      <!--刷新-->
      <refresh class="refresh" @refresh="httpMyServers" :display="isRefresh ? 'show' : 'hide'">
        <loading-indicator class="load text1"></loading-indicator>
      </refresh>

      <!--内容-->
      <cell v-for="(item, index) in servers" v-bind:key="index">

        <VServerMy v-if="item.id !== undefined" 
        class="cell" :item="item" 
        @remarkClick="remarkClick(item)" 
        @switcClick="switcClick($event,item)" 
        @renewClick="httpServerConfig(item)"
        @copyClick="copyClick(item)"></VServerMy>

        <div class="genduo-bg" v-else>
          <text class="font22 text3">{{T('没有更多数据')}}</text>
        </div>
      </cell>
    </list>

    <!-- 提示 -->
    <VTipe v-else-if="servers" :image="'collection_img_black.png'" :text="T('独享高频稳定私服，快去购买体验吧~~')"></VTipe>
  </div>
</template>

<script>

//组件
import VServerMy from '@/server/view/VServerMy.vue'
import VTipe from '@/public/view/VTipe.vue'

//功能
import modal from "@/_js/modal";
import math from "@/_js/math";
import Config from "@/_js/config";
import navigator from "@/_js/navigator";
import broadcast from "@/_js/broadcast";

import weexHttp from "@/_js/https"; ;
const clipboard = weex.requireModule("clipboard");
const weexFunction = weex.requireModule("WeexFunction");

import I18N from "@/_i18n/i18n";
var T = I18N.t;
weexFunction.setTitle(T("我的服务器"));
weexFunction.setRightBtn(T("迁移策略"));

export default {

  components: {
    VServerMy,
    VTipe,
  },

  data() {
    return {
      T:T,
      Config:Config,
      math:math,
      
      isRefresh:false, //显示刷新
      servers:undefined, //策略列表
    };
  },

  mounted() {
    
    Config.init(()=>{
      this.httpMyServers();
    },[Config.OPT_UP_DOWN]);


    broadcast.subscribe('ServerMy', 'RenewServer', ()=>{
      this.httpMyServers();
    });
  },
  
  methods: {

    //初始化数据
    initDatas(datas) {

      for (let index = 0; index < datas.length; index++) {

        //执行频率
        var data = datas[index];
        if (data.serverInfo) {
          var serverInfo = data.serverInfo;
          var pool = serverInfo.pool;
          for (let index = 0; index < pool.length; index++) {
            var map = pool[index];
            map.frequency = math.chu('60000', map.lastCircleTime, 0, 2);
          }
          data.pool = pool;
        } else {
          data.pool = [{}];
        }
        
        //折扣
        var discount = math.chu(data.currentPrice, data.originalPrice);
        discount = math.cheng(discount, '10');
        data.discount = math.xiaoshu(discount, 2, 4);

        //速度
        var num = math.getNumber(data.serverCount);
        if (num <= 20) { 
          data.speed = 1;
          data.speedText = T('高速');
        } else if (num <= 50) { 
          data.speed = 0;
          data.speedText = T('平稳');
        } else {
          data.speed = -1;
          data.speedText = T('拥挤');
        }

        //名称
        data.serverMark = data.serverMark ? data.serverMark : '';
        data.name = '#' + (data.serverMark.length ? data.serverMark : data.serverCode);
      }
      return datas;
    }, 

    /*************************************************通信触发*/
    //右按钮触发-迁移服务器
    rightBtn() {
      
      for (let index = 0; index < this.servers.length; index++) {
        var server = this.servers[index];
        if (server.serverStatus == 'ARRANGED') {
          navigator.open('', 'server/ServerMove');
          return;
        }
      }

      var parm = {'message':T('暂无部署完成的私有服务器')};
      modal.alert(parm,(bool)=>{});
    },

    /********************************** HTTP*/
    //我的服务器列表-刷新
    httpMyServers() {
      if (this.isRefresh) return;
      this.isRefresh = true;

      var param = {};
      weexHttp.post("server/queryMyServerListNew",param,true,(data) => {
          this.isRefresh = false;
          if (data.status == 200) {
            data = data.data;

            this.servers = this.initDatas(data);
            this.servers.push({});
          } else {
            modal.toast(data.msg);
          }
        }
      );
    },


    //修改备注
    httpEditMark(item, value) {

      modal.showLoad();
      var param = {'id':item.id, 'serverMark':value}
      weexHttp.postX("server/editServerMark",param,true,(data) => {
        
          modal.hideLoad();
          if (data.status == 200) {
            item.serverMark = value;
            modal.toast(T('修改成功'));
          } else {
            modal.toast(data.msg);
          }
        }
      );
    },

    //自动续费
    httpOpenAuto(item, isOn) {

      modal.showLoad(); 
      var param = {'id':item.id, 'isAutoRenewal':isOn}
      weexHttp.postX("server/openAndCloseAutoRenew",param,true,(data) => {
          modal.hideLoad();
          if (data.status == 200) {
            item.isAutoRenewal = isOn;
          } else {
            item.isAutoRenewal = !isOn;
            modal.toast(data.msg);
          }
        }
      );
    },

    //续费服务器
    httpServerConfig(item) {

      modal.showLoad(); 
      var param = {};
      weexHttp.post("server/queryServerConfigNew",param,true,(data) => {
        modal.hideLoad();
        if (data.status == 200) {
          
          var servers = data.data;
          for (let index = 0; index < servers.length; index++) {
            var server = servers[index];
            if (server.configId == item.configId) {
              var param = {
                'configId':server.configId, 
                'serverName':server.serverName,
                'datas':server.priceList, 
                'id':item.id};
              navigator.openDialog("server/ServerBuy", param);
              return;
            }
          }
          modal.toast('无法续费');
        } else {
          modal.toast(data.msg);
        }
      });
    },

    /********************************** 点击触发*/
    //备注
    remarkClick(item){

      var option = {title:T("备注"),
                   placeholder:T("请输入1-6位备注"), 
                   value:item.serverMark,
                   max:6};
      modal.prompt(option, (value)=>{
        if (!value.length) {
          modal.toast(T('输入不能为空'));
          return;
        };

        this.httpEditMark(item, value);
      });
    },

    //复制
    copyClick(item) {
      modal.toast({message:T('复制成功'), duration:2});
      clipboard.setString(item.outerIp);
    },

    //自动续费开关
    switcClick(isOn, item) {
      this.httpOpenAuto(item, isOn);
    }
  },
};

</script>


<style src="@/_css/style.css"></style>
<style scoped>
/*********************** 列表*/
.bg {
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 9;
}
.list {
  flex: 1;
  margin-top: 20px;
}
.cell {
  width: 690px;
  margin-top: 20px;
  margin-left: 30px;
  margin-bottom: 20px;
}

/*********************** 刷新*/
.refresh {
  width: 750;
  flex-direction: row;
  justify-content: center;
}
.load {
  margin-top: 16px;
  height: 40px;
  width: 40px;
}
.genduo-bg {
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 750px;
  height: 100px;
}
</style>